<?php

namespace Hdphim\Modules\Api;

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\ModuleDefinitionInterface;

class Module
{
    public function registerAutoloaders()
    {
        $loader = new Loader();
        
        $loader->registerNamespaces(array(
            'Hdphim\Modules\Api\Controllers' => __DIR__ . '/controllers/',
            'Hdphim\Modules\Api\Models' => __DIR__ . '/models/',
            'Hdphim\Modules\Api\Models\Users' => __DIR__ . '/models/users/',
            'Hdphim\Modules\Api\Plugins' => __DIR__ . '/plugins/',
            'Hdphim\Modules\Api\Forms' => __DIR__ . '/forms/',
        ));

        $loader->register();
    }
    
    public function registerServices($di)
    {
        $di['view'] = function () {
            $config = include APP_DIR . '/config/config.php';
            $view = new View();
            $view->setViewsDir(__DIR__ . '/views/');
            
            $view->setLayoutsDir('../../../views/layouts/skins/frontend/defaults/');
            $view->setPartialsDir('../../../views/layouts/skins/frontend/defaults/partials/');
            $view->setTemplateAfter('layout');
            
            $view->registerEngines(array(
		        '.volt' => function ($view, $di) use ($config) {

                    $volt = new VoltEngine($view, $di);

                    $volt->setOptions(array(
                        'compiledPath' => $config->application->cacheDir,
                        'compiledSeparator' => '_'
                    ));

                    return $volt;
                },
                '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
            ));
            return $view;
        };
        
        $di->set('dispatcher' , function(){
           $dispatcher = new \Phalcon\Mvc\Dispatcher();
           $dispatcher->setDefaultNamespace("Hdphim\Modules\Api\Controllers\\");
            return $dispatcher;
        });
    }
}